package com.xtedu.examproject.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xtedu.examproject.entity.Subject;
import com.xtedu.examproject.service.SubjectService;
import com.xtedu.examproject.util.PageHelper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/subjectmanage/*")
public class SubjectServlet extends HttpServlet {
    private SubjectService subjectService;
    public void init() {
        subjectService = new SubjectService();
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        if (pathInfo == null) {
            getAllSubject(req, resp);
            return;
        }
        if (pathInfo.equals("/subjectinfo")){
            getAllSubject(req, resp);
        }
        if (pathInfo.equals("/allsubjectinfo")){
            getAllSubjectInfo(req, resp);
        }
        if (pathInfo.equals("/delete")) {
            deleteSubject(req, resp);
        }
        if (pathInfo.equals("/select")) {
            if(req.getParameter("subjectId") != null) {
                getSubjectById(req, resp);
            }else if(req.getParameter("subjectName") != null) {
                getSubjectByName(req, resp);
            }else {
                getAllSubject(req, resp);
            }
        }

    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        if(pathInfo.equals("/update")){
            updateSubject(req, resp);
        }
        if(pathInfo.equals("/add")){
            addSubject(req, resp);
        }

    }

    public void destroy() {}

    //获取所有专业
    public void getAllSubject(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
            int pageIndex = req.getParameter("pageIndex")==null?1:Integer.parseInt(req.getParameter("pageIndex"));
            int pageSize = req.getParameter("pageSize")==null?10:Integer.parseInt(req.getParameter("pageSize"));
            PageHelper page = subjectService.findAllSubject(pageIndex, pageSize);
            page.setServletName("subjectinfo");
            HttpSession session = req.getSession();
            session.setAttribute("page", page);
            req.getRequestDispatcher("/WEB-INF/views/subjectinfo.jsp").forward(req, resp);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //根据专业编号查询
    public void getSubjectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            int subjectId;//专业编号
            try{//如果输入非数字 赋值0
                 subjectId = Integer.parseInt(req.getParameter("subjectId"));
            }catch (Exception e){
                subjectId = 0;
            }
            int pageIndex = req.getParameter("pageIndex")==null?1:Integer.parseInt(req.getParameter("pageIndex"));
            int pageSize = req.getParameter("pageSize")==null?10:Integer.parseInt(req.getParameter("pageSize"));
            PageHelper page = subjectService.findSubjectById(subjectId,pageIndex,pageSize);
            page.setServletName("select");
            HttpSession session = req.getSession();
            session.setAttribute("page", page);
            req.getRequestDispatcher("/WEB-INF/views/subjectinfo.jsp").forward(req, resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    //根据专业名称查询
    public void getSubjectByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String subjectName = req.getParameter("subjectName");
            int pageIndex = req.getParameter("pageIndex")==null?1:Integer.parseInt(req.getParameter("pageIndex"));
            int pageSize = req.getParameter("pageSize")==null?10:Integer.parseInt(req.getParameter("pageSize"));
            PageHelper page = subjectService.findSubjectByName(subjectName,pageIndex,pageSize);
            page.setServletName("select");
            HttpSession session = req.getSession();
            session.setAttribute("page", page);
            req.getRequestDispatcher("/WEB-INF/views/subjectinfo.jsp").forward(req, resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //查询所有专业信息返回json
    public void getAllSubjectInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        try {
            List<Subject> subjects;
            subjects = subjectService.findAllSubject();
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(subjects);
            resp.getWriter().write(json);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //添加专业
    public void addSubject(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        try {
            Subject subject = new Subject();
            subject.setName(req.getParameter("subjectName"));
            subject.setState(Boolean.parseBoolean(req.getParameter("subjectState")));
            int result = subjectService.insertSubject(subject);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(result);
            resp.getWriter().write(json);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //修改专业
    public void updateSubject(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        try {
            Subject subject = new Subject();
            subject.setId(Integer.parseInt(req.getParameter("subjectId")));
            subject.setName(req.getParameter("subjectName"));
            subject.setState(Boolean.parseBoolean(req.getParameter("subjectState")));
            int result = subjectService.updateSubject(subject);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(result);
            resp.getWriter().write(json);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //删除专业
    public void deleteSubject(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        try {
            int subjectId = Integer.parseInt(req.getParameter("subjectId"));
            Subject subject = new Subject();
            subject.setId(subjectId);
            int result = subjectService.deleteSubject(subject);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(result);
            resp.getWriter().write(json);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
